﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Threading;

namespace JustPlus
{
    /// <summary>
    /// ChannelPanel.xaml 的交互逻辑
    /// </summary>
    public partial class ChannelPanel : UserControl
    {
        public int startTag = 0;
        //定义tag的目的：当点击私人频段时，假入用户没有登录，需要跳转至登录界面，此时会调用此类的mouse_leave
        //函数，界面在login界面短暂停留后直接进入播放和主菜单界面，没达到要求，为了防止调用mouse_leave而定义
        //的一标志。
        public bool tag = false;
        public ChannelPanel()
        {
            InitializeComponent();
            image1.Opacity = 0.7;
            image2.Opacity = 0;
            image3.Opacity = 0;
            //GetSongList.cate = @"公共频道";
            LoadAlb(0);

            label7.Content = GetSongList.currentchannel.channelName;
        }

        private void Grid_MouseLeave(object sender, MouseEventArgs e)
        {
            if (!tag)
            {
                MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
                mwnd.stackPanel1.Children.Clear();
                mwnd.stackPanel1.Children.Add(mwnd.stackPanel2);
                mwnd.stackPanel1.Children.Add(mwnd.stackPanel3);
            }
        }

        private void LoadAlb(int startTag)
        {
            ChannelList channellist = new ChannelList();
            if (startTag <= channellist.ChannelCounts[0] - 5 && startTag >= 0)
            {
                stackPanel2.Children.Clear();
                for (int i = startTag; i <= channellist.ChannelCounts[0] - 1; i++)
                {
                    DefPictureBox defpb = new DefPictureBox();
                    defpb.Tag = i;
                    stackPanel2.Children.Add(defpb);
                }
            }
        }
        //公共频道
        private void label1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            tag = false;

            image1.Opacity = 0.7;
            image2.Opacity = 0;
            image3.Opacity = 0;

            label5.IsEnabled = true;
            label6.IsEnabled = true;

            LoadAlb(0);
        }

        //DJ频道
        private void label2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            tag = false;

            image1.Opacity = 0;
            image2.Opacity = 0.7;
            image3.Opacity = 0;

            label5.IsEnabled = false;
            label6.IsEnabled = false;

            stackPanel2.Children.Clear();
            ChannelList channellist = new ChannelList();
            for (int i = 0; i <= channellist.ChannelCounts[1] - 1; i++)
            {
                DefPictureBox defpb = new DefPictureBox();
                defpb.Tag = i + channellist.ChannelCounts[0];
                stackPanel2.Children.Add(defpb);
            }
        }

        //私人频道
        private void label3_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!LoginPanel.HasLoginOn)
            {
                tag = true;
                MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
                mwnd.stackPanel1.Children.Clear();
                LoginPanel loginpanel = new LoginPanel();
                mwnd.stackPanel1.Children.Add(loginpanel);
                return;
            }
            tag = false;
            image1.Opacity = 0;
            image2.Opacity = 0;
            image3.Opacity = 0.7;

            label5.IsEnabled = false;
            label6.IsEnabled = false;

            stackPanel2.Children.Clear();
            ChannelList channellist = new ChannelList();
            for (int i = 0; i <= channellist.ChannelCounts[2] - 1; i++)
            {
                DefPictureBox defpb = new DefPictureBox();
                defpb.Tag = i + channellist.ChannelCounts[0] + channellist.ChannelCounts[1];
                stackPanel2.Children.Add(defpb);
            }
        }

        private void label5_MouseMove(object sender, MouseEventArgs e)
        {
            startTag = startTag - 1;
            LoadAlb(startTag);
        }

        private void label6_MouseMove(object sender, MouseEventArgs e)
        {
            startTag = startTag + 1;
            LoadAlb(startTag);
        }

        //选择频道
        private void stackPanel2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
            PlayerPanel playerpanel = (PlayerPanel)mwnd.stackPanel3.Children[0];
            ThreadPool.QueueUserWorkItem(new WaitCallback((state) =>
            {
                Dispatcher.Invoke(new Action(() =>
                {

                    mwnd.stackPanel1.Children.Clear();
                    mwnd.stackPanel1.Children.Add(mwnd.stackPanel2);

                }));
                //PlayerPanel playerpanel = (PlayerPanel)mwnd.stackPanel3.Children[0];
                playerpanel.getsong.Next(true);
            }));

        }
    }
}
